<?php

/**
 * @author lzp300
 * @copyright 2015-03-26
 */

class osiiHotel{
    /**
     * 构造函数
     * @var array
     */
    /*function __construct() {
        parent::__construct();
	    
    }*/
    /**
     * 权限MD5加密
     * 测试：luo2b   luo2b   正式：key=>uni1001    id=>unibooking
     */
    function SignUtil($serviceName="1",$accountKey="luo2b",$version="1.1.0",$accountID="luo2b"){
        $reqTime=date('Y-m-d',time());
        
        $resuoce=array();
        $resuoce['state']=0;
        $resuoce['string']="";
        
        $originalArray=array();
        $originalArray['Version']=$version;
        $originalArray['AccountID']=$accountID;
        $originalArray['ReqTime']=$reqTime;
        if($serviceName){
            $originalArray['ServiceName']=$serviceName;
            ksort($originalArray);
            $sortedArray="";
            foreach($originalArray as $k=>$v){
                $sortedArray.=$k.'='.$v.'&';
            }
            $sortedArray=trim($sortedArray,'&');
            $keywords=md5($sortedArray.$accountKey);
            $resuoce['state']=1;
            $resuoce['string']['version']=$version;
            $resuoce['string']['accountId']=$accountID;
            $resuoce['string']['serviceName']=$serviceName;
            $resuoce['string']['sign']=$keywords;
            $resuoce['string']['reqTime']=$reqTime;
            //var_dump($resuoce);
        }else{
            $resuoce['string']='接口方法名错误！';
        }
        return $resuoce;
    }
    
    
    //日志记录
    function setLogs($logs){
        $fleName=$_SERVER['DOCUMENT_ROOT']."/data/Logs/".date('Y-m-d',time()).".txt";
        $th_time=fopen($fleName,"a");
        fwrite($th_time,date('Y-m-d H:i:s',time())."\r\n".$logs."\r\n\r\n\r\n");
        fclose($th_time);
    }
    /**
     * curl 传输      //测试：120.24.75.164:8999---正式：120.24.166.230:8999       192.168.1.31
     */
    function curl($post_data,$url="http://120.24.75.164:8999/PmsInterfaceConnections/PmsDataInterFace/getPmsMethod"){
        //print_r($post_data);exit;
        $ch = curl_init();
        //print_r($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_TIMEOUT,60);
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        
        $logs="调用地址：".$url."\r\n接收数据：".$return."\r\n发送数据：".$post_data;
        $this->setLogs($logs);
        
        $return=json_decode($return);
        $body=array();
        $head=array();
        if($return){
            $body=(array)$return->body;
            $head=(array)$return->head;
            foreach($body as $k=>$v){
                if(is_object($v)){
                    $body[$k]=(array)$v;
                }
            }
        //var_dump($head);exit;
        }else{
            return '{"head":"获取内容失败！"}';
        }
        return array("head"=>$head,"body"=>$body);
        //echo "<pre>";print_r($head);exit;
    }
    
    /**
     * 公共调用
     */
    function thall($actionName,$body){
        set_time_limit(0);
        $resuoce=$this->SignUtil($actionName);
        if($resuoce['state']==1){
            //echo "<pre>";echo json_encode(array("head"=>$resuoce['string'],"body"=>$body));exit;
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>$body));
            //echo '<pre>';print_r($post_data);exit();
            $contArray=$this->curl($post_data);
            $allarray['return'] = $contArray;
            $allarray['json'] = $post_data;
            return $allarray;
            //echo '<pre>';print_r($post_data);
        }else{
            return $resuoce['string'];
        }
    }
    
    /**
     * 渠道调用
     */
    function thall_qudao($actionName,$body){
        set_time_limit(0);
        $resuoce=$this->SignUtil($actionName,"osii_interface_2004_!@#","1.1.0","osii_interface_2004");
        if($resuoce['state']==1){
            //echo "<pre>";echo json_encode(array("head"=>$resuoce['string'],"body"=>$body));exit;
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>$body));
            //echo '<pre>';print_r($post_data);exit();
            $contArray=$this->curl($post_data,"http://www.wormholehotels.com/OsiiInterFace/getMethod.do");
            $allarray['return'] = $contArray;
            $allarray['json'] = $post_data;
            return $allarray;
            //echo '<pre>';print_r($post_data);
        }else{
            return $resuoce['string'];
        }
    }
    
    
    /**
     * 酒店添加、修改  发送 OSII
     */
    function hotels($body){
        $arr=$this->thall("GetHotelInfo",$body);
        return $arr;
    }
    
    /**
     * 房型添加、修改  发送OSII
     */
    function types($body){
        $arr=$this->thall("GetRoomTypeInfo",$body);
        return $arr;
    }
    
    /**
     * 图片添加、修改 发送OSII
     */
    function imgs($body){
        $arr=$this->thall("GetHotelImageInfo",$body);
        return $arr;
    }
    
    /**
     * 设施、设备  添加、修改 发送OSII
     */
    function devices($body){
        $arr=$this->thall("GetHotelFacilityInfo",$body);
        return $arr;
    }

    /**
     * 设置房态 添加、修改  发送OSII
     */
    function roomstatus($body){
        $arr=$this->thall("SetRoomInfo",$body);
        return $arr;
    }
    /**
     * 获取房型对照表
     */

    function roomTypeInfo($body){
        $arr=$this->thall("GetContrastRoomTypeInfo",$body);
        return $arr;
    }

    
    /**
     * 房型价格 添加、修改 发送OSII
     */
    function typePrice($body){
        $arr=$this->thall("SetRoomPrice",$body);
        return $arr;
    }
    
    /**
     * 设置酒店状态
     */
    function setHotelStatus($body){
        $arr=$this->thall("SetHotelStatus",$body);
        return $arr;
    }
    
    /**
     * 渠道开关
     */
    function SetDistributionChannel($body){
        $arr=$this->thall_qudao("SetDistributionChannel",$body);
        return $arr;
    }
    
    /**
     * 对订单状态进行操作(去哪儿)
     * @param type $body
     * @return type
     */
    function  OptOrder($body){
        $arr=$this->thall("OptOrder",$body);
        return $arr;
    }
    
    /**
     * 对订单状态进行操作(携程)
     * @param type $body
     * @return type
     */
    function DomesticPushOrderStatus($body){
        $arr=$this->thall("DomesticPushOrderStatus",$body);
        return $arr;
    }
}